package com.hook3.pianke.Adapter;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.hook3.pianke.R;
import com.hook3.pianke.activity.home.ChoiceActivity;

import java.io.IOException;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

/**
 * Created by HOME on 2015/12/12.
 */
public class HomeAdapter extends PagerAdapter {

    private FrameLayout frameLayout;
    private Activity activity;

    public HomeAdapter(Activity activity, FrameLayout frameLayout) {
        this.frameLayout = frameLayout;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        frameLayout.removeAllViews();
        loading_img();
        if (position == 0) {//精选
            new ChoiceActivity(activity, frameLayout);
        } else if (position == 1) {
            new ChoiceActivity(activity, frameLayout);
        } else if (position == 2) {
            new ChoiceActivity(activity, frameLayout);
        }
        container.addView(frameLayout);
        return frameLayout;
    }

    public void loading_img() {
        View inflate = View.inflate(activity, R.layout.include_loading, null);
        GifImageView gifImageView = (GifImageView) inflate.findViewById(R.id.loading_img);
        GifDrawable gifDrawable = null;
        try {
            gifDrawable = new GifDrawable(activity.getAssets(), "loading.gif");
            //gifDrawable.setCornerRadius(1.5f);
            gifDrawable.setSpeed(1.5f);
            gifImageView.setImageDrawable(gifDrawable);
        } catch (IOException e) {
            e.printStackTrace();
        }
        gifDrawable.start();
        frameLayout.addView(inflate);
    }

    //移除
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
